﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace AlarmClock
{
    public partial class Form1 : Form
    {
        private AlarmClock alarmClock;

        public Form1()
        {
            InitializeComponent();

            // 初始化闹钟实例
            alarmClock = new AlarmClock();

            // 订阅Tick事件
            alarmClock.Tick += AlarmClock_Tick;

            // 订阅Alarm事件
            alarmClock.Alarm += AlarmClock_Alarm;

            // 启动闹钟
            alarmClock.Start();

            // 初始化当前时间显示
            lblTime.Text = "当前时间: " + DateTime.Now.ToString("HH:mm:ss");
            lblStatus.Text = "状态: 等待设置闹钟";
        }

        // 每秒钟触发
        private void AlarmClock_Tick(object sender, EventArgs e)
        {
            // 更新当前时间显示
            lblTime.Text = "当前时间: " + DateTime.Now.ToString("HH:mm:ss");
            lblStatus.Text = "状态: 闹钟运行中...";
        }

        // 闹钟响铃时触发
        private void AlarmClock_Alarm(object sender, EventArgs e)
        {
            lblStatus.Text = "状态: 闹钟响铃!";
            MessageBox.Show("闹钟时间到了！", "闹钟", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // 点击按钮设置闹钟时间
        private void btnSetAlarm_Click(object sender, EventArgs e)
        {
            // 从 DateTimePicker 控件中获取用户设置的闹钟时间
            DateTime selectedTime = dtpAlarmTime.Value;

            // 如果选择的时间早于当前时间，则将闹钟设置为第二天的同一时间
            if (selectedTime <= DateTime.Now)
            {
                selectedTime = selectedTime.AddDays(1);
            }

            // 设置闹钟时间
            alarmClock.SetAlarm(selectedTime);
            lblStatus.Text = $"状态: 闹钟设置为: {selectedTime.ToString("yyyy-MM-dd HH:mm:ss")}";
        }
    }
}
